package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Flight;
import cn.wolfcode.luowowo.article.domain.Hotel;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.search.domain.*;
import cn.wolfcode.luowowo.search.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class DataController {

    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IDestinationESService destinationESService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyESService strategyESService;
    @Reference
    private ITravelService travelService;
    @Reference
    private ITravelESService travelESService;
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IUserInfoESService userInfoESService;
    @Reference
    private IHotelService hotelService;
    @Reference
    private IHotelEsService hotelEsService;
    @Reference
    private IFlightService flightService;
    @Reference
    private IFlightEsService flightEsService;
    @RequestMapping("addMg")
    @ResponseBody
    public Object addMsg(){
        StrategyComment comment = new StrategyComment();
        comment.setContent("1111");
        strategyCommentService.saveOrUpdate(comment);
        return "ok";
    }
    @RequestMapping("addES")
    @ResponseBody
    public Object addES(){
        DestinationTemplate template = new DestinationTemplate();
        template.setInfo("ssss");
        destinationESService.save(template);
        return "ok";
    }
    @RequestMapping("dataInit")
    @ResponseBody
    public Object dataInit(){
        ///攻略----------------------------------------------------------------------------
        /*
        List<StrategyDetail> all = strategyDetailService.list();
        for (StrategyDetail detail : all) {
            StrategyTemplate t = new StrategyTemplate();

            t.setId(detail.getId());
            t.setTitle(detail.getTitle());
            t.setSubTitle(detail.getSubTitle());
            t.setDestId(detail.getDest().getId());
            t.setDestName(detail.getDest().getName());
            t.setThemeId(detail.getTheme().getId());
            t.setThemeName(detail.getTheme().getName());
            t.setSummary(detail.getSummary());
            t.setCreateTime(detail.getCreateTime());
            t.setViewnum(detail.getViewnum());
            t.setFavornum(detail.getFavornum());
            t.setReplynum(detail.getReplynum());
            t.setThumbsupnum(detail.getThumbsupnum());

            t.setCoverUrl(detail.getCoverUrl());

            Destination dest = destinationService.getCountry(detail.getDest().getId());

            t.setCountryId(dest.getId());
            t.setCountryName(dest.getName());
            dest = destinationService.getProvince(detail.getDest().getId());
            if(dest != null){
                t.setProvinceId(dest.getId());
                t.setProvinceName(dest.getName());
            }

            strategyESService.save(t);
        }
        */

        ///游记----------------------------------------------------------------------------
        List<Travel> ts = travelService.list();

        for (Travel t : ts) {
            TravelTemplate tt = new TravelTemplate();
            tt.setId(t.getId());
            tt.setAuthorId(t.getAuthor().getId());
            tt.setAuthorName(t.getAuthor().getNickname());
            tt.setCoverUrl(t.getCoverUrl());
            tt.setCreateTime(t.getCreateTime());
            tt.setDestId(t.getDest().getId());
            tt.setDestName(t.getDest().getName());
            tt.setReplynum(t.getReplynum());
            tt.setSummary(t.getSummary());
            tt.setTitle(t.getTitle());
            tt.setViewnum(t.getViewnum());
            travelESService.save(tt);
        }

        //用户----------------------------------------------------------------------------
        List<UserInfo> us = userInfoService.list();
        for (UserInfo u : us) {
            UserInfoTemplate tt = new UserInfoTemplate();
            tt.setId(u.getId());
            tt.setDestName(u.getCity());
            tt.setFansnum(11);
            tt.setHeadUrl(u.getHeadImgUrl());
            tt.setInfo(u.getInfo());
            tt.setLevel(u.getLevel());
            tt.setNickname(u.getNickname());
            tt.setReplynum(10);
            tt.setTravelnum(2);
            userInfoESService.save(tt);
        }

        //目的地----------------------------------------------------------------------------
        List<Destination> ds = destinationService.list();

        for (Destination d : ds) {

            DestinationTemplate tt = new DestinationTemplate();

            tt.setId(d.getId());
            tt.setCoverUrl(d.getCoverUrl());
            tt.setName(d.getName());
            tt.setInfo(d.getInfo());
            destinationESService.save(tt);
        }


        return "ok";
    }

    @RequestMapping("/hotelInit")
    @ResponseBody
    public Object hotelInit(){
        List<Hotel> hotels = hotelService.listAll();
        Long i = 1L;
        for (Hotel hotel : hotels) {
            HotelTemplate hotelTemplate = new HotelTemplate();
            hotelTemplate.setId(i++);
            hotelTemplate.setDestId(hotel.getDestId());
            hotelTemplate.setDestName(hotel.getDestName());
            hotelTemplate.setImg(hotel.getImg());
            hotelTemplate.setName(hotel.getName());
            hotelTemplate.setEnglishName(hotel.getEnglishName());
            hotelTemplate.setScore(hotel.getScore());
            hotelTemplate.setAddress(hotel.getAddress());
            hotelTemplate.setPrice(hotel.getPrice());
            hotelTemplate.setRegionId(hotel.getRegionId());
            hotelEsService.save(hotelTemplate);
        }


        return "ok";
    }
    @RequestMapping("/flightInit")
    @ResponseBody
    public Object flightInit(){
        List<Flight> flights = flightService.query();
        //Long i = 1L;
        for (Flight flight : flights) {
            FlightTemplate flightTemplate = new FlightTemplate();
            flightTemplate.setId(flight.getId());
            flightTemplate.setDepartureCity(flight.getDepartureCity());
            flightTemplate.setDepartureDate(flight.getDepartureDate());
            flightTemplate.setDestinationCity(flight.getDestinationCity());
            flightTemplate.setNumber(flight.getNumber());
            flightTemplate.setStartAirport(flight.getStartAirport());
            flightTemplate.setEndAirport(flight.getEndAirport());
            flightTemplate.setFlightEndTime(flight.getFlightEndTime());


            flightEsService.save(flightTemplate);
        }


        return "ok";
    }
}
